package top.qitun.util;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类. <br>
 * 
 * @author lujiaxi <br>
 * @version 1.0.0 2014年6月30日 下午5:13:04 <br>
 * @see
 * @since JDK 1.4.2.6
 */
public final class DateUtil {

	public static final String FormatDT = "yyyy-MM-dd HH:mm:ss";

	public static final String FormatDTS = "yyyy-MM-dd HH:mm";

	public static final String FormatD = "yyyy-MM-dd";

	public static final String FormatT = "HH:mm:ss";

	public static Date getnow() {
		return new Date();
	}

	public static String now(final String... format) {
		return parse(new Date(), format);
	}

	public static String nowDate() {
		return now(FormatD);
	}

	public static String nowTime() {
		return now(FormatT);
	}

	public static String parse(final Date date, final String... format) {
		String f = format.length == 0 ? FormatDT : format[0];
		return new SimpleDateFormat(f).format(date);
	}

	public static Date format(final String date, final String... format) {
		String f = format.length == 0 ? FormatDT : format[0];
		try {
			return new SimpleDateFormat(f).parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	// =============取部分============

	public static int getYear(final Date... date) {
		return 0;
	}

	public static int getQuarter(final Date... date) {
		return 0;
	}

	public static int getMouth(final Date... date) {
		return 0;
	}

	public static int getDay(final Date... date) {
		return 0;
	}

	public static int getWeek(final Date... date) {
		return 0;
	}

	public static int getHour(final Date... date) {
		return 0;
	}

	public static int getMinute(final Date... date) {
		return 0;
	}

	public static int getSecond(final Date... date) {
		return 0;
	}

	// =============计算移位============

	public static Date addYear(final Date date, final int diff) {
		return null;
	}

	public static Date addQuarter(final Date date, final int diff) {
		return null;
	}

	public static Date addMouth(final Date date, final int diff) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, diff);
		return calendar.getTime();
	}

	public static Date addDay(final Date date, final int diff) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, diff);
		return calendar.getTime();
	}

	public static Date addWeek(final Date date, final int diff) {
		return null;
	}

	public static Date addHour(final Date date, final int diff) {
		return null;
	}

	public static Date addMinute(final Date date, final int diff) {
		return null;
	}

	public static Date addSecond(final Date date, final int diff) {
		return null;
	}

	// =============计算间隔============

	public static int distanceQuarter(final Date previous, final Date next) {
		return 0;
	}

	public static int distanceMonth(final Date previous, final Date next) {
		return 0;
	}

	public static long distanceDay(final Date previous, final Date next) {
		long distance = distanceMilliSecond(previous, next);
		return distance / (1000 * 3600 * 24);
	}

	public static int distanceWeek(final Date previous, final Date next) {
		long distance = distanceMilliSecond(previous, next);
		return (int) (distance / (1000 * 3600 * 24 * 7));
	}

	public static long distanceHour(final Date previous, final Date next) {
		long distance = distanceMilliSecond(previous, next);
		return distance / (1000 * 3600);
	}

	public static long distanceMinute(final Date previous, final Date next) {
		long distance = distanceMilliSecond(previous, next);
		return distance / (1000 * 60);
	}

	public static long distanceSecond(final Date previous, final Date next) {
		long distance = distanceMilliSecond(previous, next);
		return distance / 1000;
	}

	private static long distanceMilliSecond(final Date previous, final Date next) {
		long distance = 0L;
		if (previous != null && next != null) {
			distance = next.getTime() - previous.getTime();
		}
		return distance;// 如果是负数就返回负数呗，不要转为绝对值
	}

	// =============特殊============

	public static boolean isLeapYear(final Date... date) {
		return false;
	}

	public static int dayCountInMonth(final Date... date) {
		return 0;
	}

	public static int weekCountInMonth(final Date... date) {
		return 0;
	}

	/**
	 * 方法表述:取给定日期的零点时间
	 * 
	 * Date
	 */
	public static Date getDateZeroTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		getDateZeroTime(calendar);
		return calendar.getTime();
	}

	/**
	 * 方法表述:取给定日期的临界时间（最后的时间）
	 * 
	 * Date
	 */
	public static Date getCriticalTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}

	/**
	 * 方法表述:取给定日期的零点时间
	 * 
	 * @param calendar
	 * @return Calendar
	 */
	public static Calendar getDateZeroTime(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar;
	}

	/**
	 * 方法表述:日期对象转字符串
	 * 
	 * @param date
	 * @param formatter
	 * @return String
	 */
	public static String date2Str(Date date, SimpleDateFormat formatter) {
		return formatter.format(date);
	}

	/**
	 * 方法表述:日期对象转字符串
	 * 
	 * @param date
	 * @param formatter
	 * @return String
	 */
	public static String date2Str(Date date) {
		if (date != null) {
			DateFormat dataFormat = new SimpleDateFormat(FormatDT);
			return dataFormat.format(date);
		}
		return null;

	}

	/**
	 * 方法表述:字符串对象转日期
	 * 
	 * @param dateStr
	 * @param formatter
	 * @return
	 * @throws ParseException
	 *             Date
	 */
	public static Date str2Date(String dateStr, SimpleDateFormat formatter) throws ParseException {
		return formatter.parse(dateStr);
	}

	public static Date str2Date(String dateStr) throws ParseException {
		DateFormat dateFormat = new SimpleDateFormat(FormatDT);
		return dateFormat.parse(dateStr);
	}

//	public static String convertGmtDateStr(String _timeZone, Date date) {
//		TimeZone timeZone = null;
//		if (StringUtils.isEmpty(_timeZone)) {
//			timeZone = TimeZone.getDefault();
//		} else {
//			timeZone = TimeZone.getTimeZone(_timeZone);
//		}
//
//		SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FormatDT);
//		sdf.setTimeZone(timeZone);
//		return sdf.format(date);
//	}

	/**
	 * 计算两个日期相差多少天，支持跨年
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getDaysBetween(Calendar start, Calendar end) {
		if (start.after(end)) {
			Calendar swap = start;
			start = end;
			end = swap;
		}
		int days = end.get(Calendar.DAY_OF_YEAR) - start.get(Calendar.DAY_OF_YEAR);
		int y2 = end.get(Calendar.YEAR);
		if (start.get(Calendar.YEAR) != y2) {
			start = (Calendar) start.clone();
			do {
				days += start.getActualMaximum(Calendar.DAY_OF_YEAR);
				start.add(Calendar.YEAR, 1);
			} while (start.get(Calendar.YEAR) != y2);
		}
		return days;
	}

	/**
	 * 方法表述:毫秒时间装换成字符串时间
	 * 
	 */
	public static String getDateStrByMillionSecond(long millionSecond) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millionSecond);
		return DateUtil.date2Str(calendar.getTime());
	}

	/**
	 * 方法表述:毫秒时间装换成字符串时间
	 * 
	 */
	public static Date getDateByMillionSecond(long millionSecond) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millionSecond);
		return calendar.getTime();
	}

	/**
	 * 获取Date的1970毫秒数
	 * 
	 */
	public static long getMillionSecondByDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getTimeInMillis();
	}

	public static boolean isSameTodayDay(Date date) {
		String nowDate = DateUtil.nowDate();
		String answerDate = DateUtil.parse(date, DateUtil.FormatD);
		return nowDate.equals(answerDate);
	}

	public static void main(String[] args) {
		long c = DateUtil.getCurrentSeconds();
		System.out.println(c);
	}

	public static int getCurrentSeconds() {
		Calendar calerdar = Calendar.getInstance();
		int hour = calerdar.get(Calendar.HOUR_OF_DAY);
		int minuts = calerdar.get(Calendar.MINUTE);
		int seconds = calerdar.get(Calendar.SECOND);
		int currentSconds = hour * 60 * 60 + minuts * 60 + seconds;
		return currentSconds;
	}

	public static LocalDateTime getFirstDayWeek() {
		LocalDateTime now = LocalDateTime.now();
		DayOfWeek dayOfWeek = now.getDayOfWeek();
		LocalDateTime firstDay = now.minusDays(dayOfWeek.ordinal());
		return firstDay;
	}

	public static LocalDateTime getLastDayOfWeek() {
		LocalDateTime now = LocalDateTime.now();
		DayOfWeek dayOfWeek = now.getDayOfWeek();
		LocalDateTime lastDay = now.plusDays(DayOfWeek.SUNDAY.getValue() - dayOfWeek.ordinal() - 1);
		return lastDay;
	}

	/**
	 * 
	 * </p>
	 * 获取两个时间相差的天数
	 * </p>
	 * 
	 * @param beginTime
	 * @param endTime
	 * @return
	 * @author clf
	 * @throws ParseException
	 * @throws UnsupportedEncodingException 
	 * @date 2017年6月3日
	 */
	public static int getDiffDay(String beginTime, Date endTime) throws ParseException, UnsupportedEncodingException {
	    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(formatter.parse(beginTime));
		int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
		aCalendar.setTime(endTime);
		int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
		return day2 - day1;

	}
}
